package org.deadline.model.baseapp;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.deadline.model.config.EnumerationDataOnDemand;
import org.deadline.repository.baseapp.CommunicationChannelRepository;
import org.deadline.service.baseapp.CommunicationChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Configurable
@Component
public class CommunicationChannelDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<CommunicationChannel> data;

	@Autowired
    private AddressDataOnDemand addressDataOnDemand;

	@Autowired
    private EnumerationDataOnDemand enumerationDataOnDemand;

	@Autowired
    private PrincipalDataOnDemand principalDataOnDemand;

	@Autowired
    CommunicationChannelService communicationChannelService;

	@Autowired
    CommunicationChannelRepository communicationChannelRepository;

	public CommunicationChannel getNewTransientCommunicationChannel(int index) {
        CommunicationChannel obj = new CommunicationChannel();
        setAreaCode(obj, index);
        setConfigValues(obj, index);
        setCountryCode(obj, index);
        setCreationDate(obj, index);
        setDeleted(obj, index);
        setExtension(obj, index);
        setModificationDate(obj, index);
        setPrimaryContact(obj, index);
        return obj;
    }

	public void setAreaCode(CommunicationChannel obj, int index) {
        String areaCode = "areaCode_" + index;
        if (areaCode.length() > 30) {
            areaCode = areaCode.substring(0, 30);
        }
        obj.setAreaCode(areaCode);
    }

	public void setConfigValues(CommunicationChannel obj, int index) {
        String configValues = "configValues_" + index;
        if (configValues.length() > 30) {
            configValues = configValues.substring(0, 30);
        }
        obj.setConfigValues(configValues);
    }

	public void setCountryCode(CommunicationChannel obj, int index) {
        String countryCode = "countryCode_" + index;
        if (countryCode.length() > 30) {
            countryCode = countryCode.substring(0, 30);
        }
        obj.setCountryCode(countryCode);
    }

	public void setCreationDate(CommunicationChannel obj, int index) {
        Date creationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setCreationDate(creationDate);
    }

	public void setDeleted(CommunicationChannel obj, int index) {
        Boolean deleted = true;
        obj.setDeleted(deleted);
    }

	public void setExtension(CommunicationChannel obj, int index) {
        String extension = "extension_" + index;
        if (extension.length() > 30) {
            extension = extension.substring(0, 30);
        }
        obj.setExtension(extension);
    }

	public void setModificationDate(CommunicationChannel obj, int index) {
        Date modificationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setModificationDate(modificationDate);
    }

	public void setPrimaryContact(CommunicationChannel obj, int index) {
        Boolean primaryContact = true;
        obj.setPrimaryContact(primaryContact);
    }

	public CommunicationChannel getSpecificCommunicationChannel(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        CommunicationChannel obj = data.get(index);
        Long id = obj.getId();
        return communicationChannelService.findCommunicationChannel(id);
    }

	public CommunicationChannel getRandomCommunicationChannel() {
        init();
        CommunicationChannel obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return communicationChannelService.findCommunicationChannel(id);
    }

	public boolean modifyCommunicationChannel(CommunicationChannel obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = communicationChannelService.findCommunicationChannelEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'CommunicationChannel' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<CommunicationChannel>();
        for (int i = 0; i < 10; i++) {
            CommunicationChannel obj = getNewTransientCommunicationChannel(i);
            try {
                communicationChannelService.saveCommunicationChannel(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            communicationChannelRepository.flush();
            data.add(obj);
        }
    }
}
